package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.projectDeclare.oneProDeclare.BuildDeclareDto;
import com.dg.cuckoo.admin.repository.BuildDeclareRepository;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.service.BuildDeclareService;
import com.dg.cuckoo.admin.service.FileService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BuildDeclareServiceimpl implements BuildDeclareService {

    @Autowired
    private BuildDeclareRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileService fileService;
    @Autowired
    private FileRepository fileRepository;

    @Transactional
    @Override
    public void save(BuildDeclareDto dto, MultipartFile[] files) throws Exception {
        dto = checkSave(dto);
        BuildDeclareDto save = repository.save(dto);
        if (files != null) {
            projectInfoServer.powerfulFileSave(files, save.getId());
        }
    }

    @Override
    public void delect(String id) {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
    }

    @Override
    public String openInfo(String id) {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
        BuildDeclareDto dto = repository.findById(id).get();
        List<String> files = fileService.byByMark(id);
        JSONObject json = new JSONObject();
        json.put("dto", dto);
        json.put("files", files);
        return json.toString();
    }

    @Override
    public PageModel page(Integer page, Integer pageSize) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findAll().size();
        List<BuildDeclareDto> dtos = repository.page(page, pageSize);
        return new PageModel(one, pageSize, size, dtos);
    }

    @Override
    public void update(String id, BuildDeclareDto dto, MultipartFile[] files) throws Exception {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
//        dto=checkSave(dto);
        dto.setId(id);
        BuildDeclareDto save = repository.save(dto);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
        if (files != null) {
            projectInfoServer.powerfulFileSave(files, save.getId());
        }
    }

    public BuildDeclareDto checkSave(BuildDeclareDto dto) {
        if (dto.getProjectName() == null && dto.getProjectName().equals("")) {
            throw new ServiceException("请选择项目名称");
        }
        if (repository.findByProjectName(dto.getProjectName()) != null) {
            throw new ServiceException("项目名称已存在");
        }
//        dto.setTotalAmount(new BigDecimal(dto.getCityFinancial()).add(new BigDecimal(dto.getTownFinancial())).add(new BigDecimal(dto.getSuperiorSubsidy())).add(new BigDecimal(dto.getFinancing())).add(new BigDecimal(dto.getSelfFunded())).toString());
//        dto.setTotalGround(new BigDecimal(dto.getSpatialIndex()).add(new BigDecimal(dto.getLandIndex())).add(new BigDecimal(dto.getBuilding_area())).add(new BigDecimal(dto.getYearSpatialIndex())).add(new BigDecimal(dto.getYearLandIndex())).toString());
//        dto.setTotalYearAmount(new BigDecimal(dto.getLevelAmount()).add(new BigDecimal(dto.getTownAmount())).add(new BigDecimal(dto.getUpAmount())).add(new BigDecimal(dto.getFinancingAmount())).add(new BigDecimal(dto.getFundedAmount())).add(new BigDecimal(dto.getOneSeasonAmount())).add(new BigDecimal(dto.getTwoSeasonAmount())).add(new BigDecimal(dto.getThrSeasonAmount())).add(new BigDecimal(dto.getFourSeasonAmount())).toString());
//        dto.setTotalDefray(new BigDecimal(dto.getYearLevelAmount()).add(new BigDecimal(dto.getYearTownAmount())).add(new BigDecimal(dto.getYearUpAmount())).add(new BigDecimal(dto.getYearFinancingAmount())).add(new BigDecimal(dto.getYearFundedAmount())).toString());
        return dto;
    }
}
